package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.EachTypeImagesMapper;
import com.zygh.policeasset.pojo.EachTypeImages;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/EachTypeImagesService.class */
public class EachTypeImagesService {
    @Autowired
    EachTypeImagesMapper eachTypeImagesMapper;
    @Autowired
    MinioService minioService;
    private String JpgContentType = "image/jpeg";

    public CommonResult whenUpateImage(EachTypeImages eachTypeImages) {
        try {
            String objectName = eachTypeImages.getType() + "/" + eachTypeImages.getTypeId() + "/" + IdUtil.objectId() + ".jpg";
            this.minioService.upload(objectName, eachTypeImages.getIcon().getInputStream(), this.JpgContentType, "assets");
            if (this.eachTypeImagesMapper.add(eachTypeImages.getTypeId(), "http://41.6.251.17:9000/assets/" + objectName, DateUtil.now(), eachTypeImages.getType()).intValue() > 0) {
                return new CommonResult().success();
            }
            return new CommonResult().validateFailed("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public CommonResult whenAddImage(EachTypeImages eachTypeImages) {
        String objectName = eachTypeImages.getType() + "/" + eachTypeImages.getTypeId() + "/" + IdUtil.objectId() + ".jpg";
        try {
            this.minioService.upload(objectName, eachTypeImages.getIcon().getInputStream(), this.JpgContentType, "assets");
            return new CommonResult().success("http://41.6.251.17:9000/assets/" + objectName);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer delete(Integer id) {
        int count = 0;
        try {
            this.minioService.removeObject("assets", this.eachTypeImagesMapper.findById(id).getConnect().split("http://41.6.251.17:9000/assets/")[1]);
            count = this.eachTypeImagesMapper.delete(id);
            return count;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return count;
        }
    }

    public List<EachTypeImages> findbyTypeIdAndType(Integer typeId, String type) {
        return this.eachTypeImagesMapper.findbyTypeIdAndType(typeId, type);
    }
}